//
//  CCAPublicViewController.m
//  Activity
//
//  Created by coco on 15/5/10.
//  Copyright (c) 2015年 coco. All rights reserved.
//

#import "CCAPublicViewController.h"
#import "CCImageCell.h"
#import  "QBImagePickerController.h"

@interface CCAPublicViewController ()
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIView *containView;
@property (nonatomic, strong) UIToolbar *bar;
@property (nonatomic, strong) UITextView *btitle;
@property (nonatomic, strong) UIView *sepTitle;
@property (nonatomic, strong) UITextField *address;
@property (nonatomic, strong) UIView *sepAddress;
@property (nonatomic, strong) UILabel *location;
@property (nonatomic, strong) UIButton *locationButton;
@property (nonatomic, strong) UILabel *locationLabel;
@property (nonatomic, strong) UILabel *startTime;
@property (nonatomic, strong) UILabel *endTime;
@property (nonatomic, strong) UILabel *durationLabel;
@property (nonatomic, strong) UIView *dateView;
@property (nonatomic, strong) UIView *sepTime;
@property (nonatomic, strong) UITextField *people;
@property (nonatomic, strong) UIView *sepPeople;
@property (nonatomic, strong) CCImageCollectionView *imageCollection;
@property (nonatomic, strong) UIView *sepImage;
@property (nonatomic, strong) UITextView *contentView;
@property (nonatomic, strong) CCAPublicViewModel *viewModel;
@property (nonatomic, assign) BOOL isStart;
@end

@implementation CCAPublicViewController

- (id) initWithViewModel:(CCAPublicViewModel *)viewModel {
  if (self = [super init]) {
    _viewModel = viewModel;
  }
  return self;
}

- (void)viewDidLoad {
  [self setupNaviBar];
  [self setupUI];
  [self setupImagePicker];
  [self setupCSS];
  [self bindUI];
  [super viewDidLoad];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void) setupNaviBar {
  self.title = @"发活动";
  UIButton *publicButton = [UIButton buttonWithType:UIButtonTypeCustom];
  [publicButton setTitle:@"发布" forState:UIControlStateNormal];
  [publicButton setTitleColor:RGB(0,0,0,1) forState:UIControlStateNormal];
  publicButton.frame = CGRectMake(0, 0, 60, 40);
  UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:publicButton];
  self.navigationItem.rightBarButtonItem = item;
}
- (void) setupUI {
  _scrollView = [UIScrollView new];
  [self.view addSubview:_scrollView];
  
  _containView = [UIView new];
  [self.scrollView addSubview:_containView];

  _btitle = [[UITextView alloc] init];
  _btitle.text = @"标题";
  _btitle.font = [UIFont boldSystemFontOfSize:19];
  _sepTitle = [UIView new];
  _sepTitle.backgroundColor = RGB(225,225,225,1);
  
  _address = [[UITextField alloc] init];
  _address.text = @"地址";
  _sepAddress =  [UIView new];
    _sepAddress.backgroundColor = RGB(225,225,225,1);
  _locationButton = [UIButton buttonWithType:UIButtonTypeCustom];
  [_locationButton setImage:IMAGE(@"g_address") forState:UIControlStateNormal];
  _locationLabel = [UILabel new];
  _locationLabel.text = @"清华信息港A座405";
  _locationLabel.font = [UIFont systemFontOfSize:12];
  _locationLabel.textColor = [UIColor blueColor];
  _startTime = [UILabel new];
  _startTime.text = @"2014/13/23";
  _endTime = [UILabel new];
  _endTime.text = @"2014/13/24";
  _endTime.textAlignment = NSTextAlignmentRight;
  _durationLabel = [UILabel new];
  _durationLabel.text = @"~";
  _sepTime = [UIView new];
  _sepTime.backgroundColor = RGB(225,225,225,1);
  _people = [[UITextField alloc] init];
  _people.placeholder = @"请填写活动人数";
  _sepPeople = [UIView new];
  _sepPeople.backgroundColor = RGB(225,225,225,1);
  _sepImage = [UIView new];
  _sepImage.backgroundColor = RGB(225,225,225,1);
  _contentView = [UITextView new];
  _contentView.text = @"活动内容";
  [self.containView addSubview: _btitle];
  [self.containView addSubview:_sepTitle];
  [self.containView addSubview:_address];
  [self.containView addSubview:_locationButton];
  [self.containView addSubview:_locationLabel];
  [self.containView addSubview:_sepAddress];
  [self.containView addSubview:_startTime];
  [self.containView addSubview: _durationLabel];
  [self.containView addSubview:_endTime];
  [self.containView addSubview:_sepTime];
  [self.containView addSubview:_people];
  [self.containView addSubview:_sepPeople];
  [self.containView addSubview:_sepImage];
  [self.containView addSubview:_contentView];
}

- (void) setupImagePicker {
  _imageCollection = [[CCImageCollectionView alloc] initWithDataSourece:self];
  [self.containView addSubview:_imageCollection];
  [_imageCollection mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.containView).offset(10);
    make.right.equalTo(self.containView).offset(-10);
    make.top.equalTo(self.sepPeople).offset(5);
    make.width.mas_equalTo(300);
  }];
}
- (void) setupCSS {
  
  [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.leading.equalTo(self.view);
    make.right.equalTo(self.view);
    make.top.equalTo(self.view).offset(self.yoffset);
    make.bottom.equalTo(self.view);
  }];

  
  CGFloat titleHeight = [self.viewModel setTitleLineNumber:self.btitle.text font:self.btitle.font];
  [_btitle mas_updateConstraints:^(MASConstraintMaker *make) {
    make.leading.equalTo(self.containView).offset(10);
    make.right.equalTo(self.containView).offset(-10);
    make.top.equalTo(self.containView).offset(10);
    make.height.mas_equalTo(titleHeight);
  }];
  [_sepTitle mas_makeConstraints:^(MASConstraintMaker *make) {
    make.leading.equalTo(self.containView).offset(10);
    make.right.equalTo(self.containView).offset(-10);
    make.top.equalTo(self.btitle.mas_bottom).offset(5);
    make.height.mas_equalTo(0.5f);
  }];
  
  [_address mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.containView).offset(10);
    make.right.equalTo(self.locationButton.mas_left).offset(-10);
    make.top.equalTo(self.sepTitle.mas_bottom).offset(10);
    make.height.mas_equalTo(30);
  }];
  [_locationButton mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.sepTitle.mas_bottom).offset(10);
    make.size.mas_equalTo(CGSizeMake(30, 30));
    make.right.equalTo(self.containView).offset(-10);
  }];
  [_locationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    make.leading.equalTo(self.containView).offset(10);
    make.right.equalTo(self.containView).offset(-10);
    make.top.equalTo(self.address.mas_bottom).offset(3);
  }];
  [_sepAddress mas_makeConstraints:^(MASConstraintMaker *make) {
    make.leading.equalTo(self.containView).offset(10);
    make.right.equalTo(self.containView).offset(-10);
    make.top.equalTo(self.locationLabel.mas_bottom).offset(5);
    make.height.mas_equalTo(0.5f);
  }];
  
  [_durationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    make.centerX.equalTo(self.containView);
    make.top.equalTo(self.sepAddress).offset(5);
  }];
  [_startTime mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.containView).offset(10);
    make.right.equalTo(self.durationLabel.mas_left).offset(-2);
    make.top.equalTo(self.sepAddress).offset(5);
  }];
  [_endTime mas_makeConstraints:^(MASConstraintMaker *make) {
    make.right.equalTo(self.containView).offset(-10);
    make.left.equalTo(self.durationLabel.mas_right).offset(2);
    make.top.equalTo(self.sepAddress).offset(5);
    
  }];
  
  [_sepTime mas_makeConstraints:^(MASConstraintMaker *make) {
    make.leading.equalTo(self.containView).offset(10);
    make.right.equalTo(self.containView).offset(-10);
    make.top.equalTo(self.endTime.mas_bottom).offset(5);
    make.height.mas_equalTo(0.5f);
  }];
  
  [_people mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.containView).offset(10);
    make.right.equalTo(self.locationButton.mas_left).offset(-10);
    make.top.equalTo(self.sepTime.mas_bottom).offset(10);
    make.height.mas_equalTo(30);
  }];
  [_sepPeople mas_makeConstraints:^(MASConstraintMaker *make) {
    make.leading.equalTo(self.containView).offset(10);
    make.right.equalTo(self.containView).offset(-10);
    make.top.equalTo(self.people.mas_bottom).offset(5);
    make.height.mas_equalTo(0.5f);
  }];
  [_sepImage mas_makeConstraints:^(MASConstraintMaker *make) {
    make.leading.equalTo(self.containView).offset(10);
    make.right.equalTo(self.containView).offset(-10);
    make.top.equalTo(self.imageCollection.mas_bottom).offset(5);
    make.height.mas_equalTo(0.5f);
  }];
  [_contentView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.leading.equalTo(self.containView).offset(10);
    make.right.equalTo(self.containView).offset(-10);
    make.top.equalTo(self.sepImage.mas_bottom).offset(5);
    make.height.mas_equalTo(100);

  }];
  [self.containView mas_remakeConstraints:^(MASConstraintMaker *make) {
    make.edges.equalTo(self.scrollView);
    make.width.equalTo(self.scrollView);
    make.bottom.equalTo(_contentView.mas_bottom);
  }];

}

- (void) bindUI {
  [self.btitle.rac_textSignal subscribeNext:^(id x) {
    [[self.viewModel titleHeight:_btitle.text font:_btitle.font] subscribeNext:^(NSNumber *x) {
      [_btitle mas_updateConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.containView).offset(10);
        make.right.equalTo(self.containView).offset(-10);
        make.top.equalTo(self.containView).offset(5);
        make.height.mas_equalTo(x.floatValue);
      }];
      if (x.floatValue > _btitle.frame.size.height) {
        [self.btitle scrollRangeToVisible:NSMakeRange(1,3)];
      }
    }];
  }];
  
  UITapGestureRecognizer *startTap = [[UITapGestureRecognizer alloc] init];
  [self.startTime addGestureRecognizer:startTap];
  self.startTime.userInteractionEnabled = YES;
  [startTap.rac_gestureSignal subscribeNext:^(id x) {
    self.isStart = YES;
    [self showTime];
  }];

  UITapGestureRecognizer *endTap = [[UITapGestureRecognizer alloc] init];
  [self.endTime addGestureRecognizer:endTap];
  [endTap.rac_gestureSignal subscribeNext:^(id x) {
    self.isStart = NO;
    [self showTime];
  }];
  RAC(self.viewModel, startDate) = RACObserve(self.startTime, text);
  RAC(self.viewModel, endDate) = RACObserve(self.endTime, text);
  RAC(self.viewModel, title) = self.btitle.rac_textSignal;
  RAC(self.viewModel, people) = self.people.rac_textSignal;
  RAC(self.viewModel, content) = self.contentView.rac_textSignal;
}

- (void) showTime {
  UIDatePicker *datePicker = [[UIDatePicker alloc] init];
  self.dateView = [UIView new];
  self.dateView.tag = 111;
  [self.view addSubview:self.dateView];
  [self.dateView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.view);
    make.right.equalTo(self.view);
    make.height.mas_equalTo(200);
    make.bottom.equalTo(self.view);
  }];
  [self.dateView addSubview:datePicker];
  
  UIToolbar *toolBar = [[UIToolbar alloc] init];
  UIButton *okButton = [UIButton buttonWithType:UIButtonTypeCustom];
  okButton.frame = CGRectMake(0, 0, 60, 40);
  [okButton setTitle:@"确定" forState:UIControlStateNormal];
  [okButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
  okButton.rac_command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
    if (_isStart) {
      _startTime.text = [datePicker.date description];
    } else {
      _endTime.text = [datePicker.date description];
    }
    [self hiddenTime];
    return [RACSignal empty];
  }];
  UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
  cancelButton.rac_command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
    [self hiddenTime];
    return [RACSignal empty];
  }];
  cancelButton.frame = CGRectMake(0, 0, 60, 40);
  [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
  [cancelButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
  UIBarButtonItem *okItem = [[UIBarButtonItem alloc] initWithCustomView:okButton];
  UIBarButtonItem *cancelItem = [[UIBarButtonItem alloc] initWithCustomView:cancelButton];
  UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
  toolBar.items = @[space, okItem, cancelItem];
  [self.view addSubview:toolBar];
  
  [toolBar mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.dateView);
    make.right.equalTo(self.dateView);
    make.top.equalTo(self.dateView);
  }];
  
  [datePicker mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.dateView);
    make.right.equalTo(self.dateView);
    make.top.equalTo(toolBar);
  }];
}


- (void) hiddenTime {
  [self.dateView removeFromSuperview];
}


#pragma  mark 
- (NSArray *) itemOfCollection {
  NSMutableArray *arr = [NSMutableArray new];
  UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  button.rac_command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
    QBImagePickerController *imagePickerController = [QBImagePickerController new];
    imagePickerController.delegate = self;
    imagePickerController.allowsMultipleSelection = YES;
    imagePickerController.showsNumberOfSelectedAssets = YES;
    
    [self presentViewController:imagePickerController animated:YES completion:NULL];
    return [RACSignal empty];
  }];
  [button setImage:IMAGE(@"compose_pic_add") forState:UIControlStateNormal];
  [arr addObject: button];
  return arr;
}

#pragma mark 
- (void)qb_imagePickerController:(QBImagePickerController *)imagePickerController didSelectAssets:(NSArray *)assets {
  NSMutableArray *arr = [NSMutableArray new];
  for (ALAsset *asset in assets) {
    CGImageRef thum = [asset thumbnail];
    UIImage* ti = [UIImage imageWithCGImage:thum];
    CCImageCell *view = [[CCImageCell alloc] initWithImage:ti closeImage:IMAGE(@"compose_photo_close")];
    __weak CCImageCell *weakView = view;
    @weakify(self);
    [view setCloseBlock:^{
      @strongify(self);
      [_imageCollection deleItem:weakView];
      [self.viewModel.imageArray removeObject:weakView.imageView.image];
    }];
    [self.viewModel.imageArray addObject:ti];
    [arr addObject:view];
  }
  [_imageCollection addItems:arr];
  [imagePickerController dismissViewControllerAnimated:YES completion:NULL];
}
- (void)qb_imagePickerControllerDidCancel:(QBImagePickerController *)imagePickerController {
  [imagePickerController dismissViewControllerAnimated:YES completion:NULL];
}

@end

// 地点 、人数、时间、报名费、